<template>
  <div class="my-container">
    <!-- 已经登录 -->
    <div class="header user-info" v-if="user">
      <!-- 两部分 一个用户信息  一个数据信息 -->
      <div class="base-info">
        <div class="left">
          <van-image :src="userInfo.photo" class="avatar" round fit="cover" />
          <span class="name">{{userInfo.name}}</span>
        </div>
        <div class="right">
          <van-button size="mini" round   to="/user/profile"  type="default" >编辑资料</van-button>
        </div>
      </div>
      <div class="data-stats">
        <div class="data-item">
          <span class="count">{{ userInfo.art_count }}</span>
          <span class="count">头条</span>
        </div>
        <div class="data-item">
          <span class="count">{{ userInfo.follow_count }}</span>
          <span class="count">关注</span>
        </div>
        <div class="data-item">
          <span class="count">{{ userInfo.fans_count }}</span>
          <span class="count">粉丝</span>
        </div>
        <div class="data-item">
          <span class="count">{{ userInfo.like_count }}</span>
          <span class="count">获赞</span>
        </div>
      </div>
    </div>
    <!-- 未登录状态 -->
    <div class="header not-login" v-else>
      <!-- $router.push('/login')"路径跳跃  -->
      <div class="login-btn" @click="$router.push('/login')">
        <img class="mobile-img" src="~@/assets/mobile.png" />
        <span class="text">登录/注册</span>
      </div>
    </div>

    <!-- 宫格导航   :column-num="2"自定义列数 -->
    <!-- slot="text"自定义文字  slot="icon" 自定义图标 clickable 是否开启格子点击反馈有  -->
    <van-grid :column-num="2" class="nav" clickable>
      <van-grid-item class="nav-two">
        <i slot="icon" class="iconfont icon-shoucang"></i>
        <span slot="text" class="text">收藏</span>
      </van-grid-item>
      <van-grid-item class="nav-two">
        <i slot="icon" class="iconfont icon-lishi"></i>
        <span slot="text" class="text">历史</span>
      </van-grid-item>
    </van-grid>
    <!-- 单元格导航 -->
    <van-cell title="消息通知" is-link />
    <van-cell title="小智同学" is-link />
    <!-- 退出登录  -->
    <van-cell
      v-if="user"
      class="logout-cell"
      title="退出登录"
      center
      @click="onLout"
    />

    <div class="grid-nav"></div>
    <van-cell title="消息通知" is-link url="" />
    <van-cell title="实名认证" is-link url="" />
    <van-cell title="用户反馈" is-link url="" />
    <van-cell title="小智同学" is-link url="" />
    <van-cell title="系统设置" is-link url="" />
  </div>
</template>

<script>
import { getUserInfo } from "@/api/user";//加载
import { mapState } from "vuex";
export default {
  name: "MyPage",
  components: {},
  props: {},
  data() {
    return {
      userInfo: {}, //用户信息
    };
  },
  computed: {
    ...mapState(["user"]),
  },
  watch: {},
  created() {
    // 如果有登录信息就调用 没有就不要

    if (this.user) {
      // 从容器中映射来的用户信息
      this.loadUserInfo();
    }
  },
  mounted() {},
  methods: {
    // 给退出登录绑定点击事件
    onLout() {
      // ...mapMutations(['setUser'])
      //this.$dialog  提示是否退出登录
      this.$dialog
        .confirm({
          title: "确认退出吗",
        })
        //清除登录状态的user和本地存储的user
        .then(() => {
          // on confirm
          console.log("确认成功");
          this.$store.commit("setUser", null);
        })
        .catch(() => {
          // on cancel
          console.log("确认取消");
        });
    },
    // 封装请求加载数据
    async loadUserInfo() {
      try {
        const { data } = await getUserInfo();
        console.log(data);
        this.userInfo = data.data;
      } catch (err) {
        this.$toast("后台崩溃");
      }
    },
  },
};
</script>

<style scoped lang="less">
// 未登录样式
.my-container {
  > .header {
    width: 750px;
    height: 361px;
    background: url("~@/assets/banner.png");
    background-size: cover;
  }
  .not-login {
    display: flex;
    // 水平居中
    justify-content: center;
    // 垂直居中
    align-items: center;
    .login-btn {
      display: flex;
      align-items: center;
      // 垂直排列
      flex-direction: column;
      .mobile-img {
        width: 132px;
        height: 132px;
        margin-bottom: 15px;
      }
      .text {
        font-size: 28px;
        color: #fff;
      }
    }
  }
}
// 已经登录状态
.user-info {
  background: red;
  .base-info {
    height: 231px;
    padding: 76px 32px 23px;

    background: #ccc;
    display: flex;
    justify-content: space-between;
    align-items: center;
    box-sizing: border-box;
    .left {
      display: flex;
      align-items: center;
      .avatar {
        width: 132px;
        height: 132px;
        border: 2px solid #fff;
        margin-right: 23px;
      }
      .name {
        font-size: 29px;
        margin-left: 22px;
      }
    }
  }
  .data-stats {
    // height: 130px;
    // background-color: #ccc;
    display: flex;
    .data-item {
      flex: 1;
      height: 130px;
      display: flex;
      justify-content: center;
      align-items: center;
      flex-direction: column;
      color: #fff;
      .count {
        font-size: 36px;
      }
      .text {
        font-size: 23px;
      }
    }
  }
}
//宫格导航样式
.nav {
  .nav-two {
    height: 141px;
    i.iconfont {
      font-size: 45px;
    }
    .icon-shoucang {
      color: #eb5253;
    }
    .icon-lishi {
      color: #ff9d1d;
    }
    span.text {
      font-size: 28px;
    }
  }
}
//单元格导航
.logout-cell {
  height: 100px;
  text-align: center;
  color: #eb5253;
  margin: 10px 0;
  font-size: 30px;
}
</style>